﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Ana_Form
{
    class Form_Model : Form
    {
        DepodataDataContext db;
        Model model;
        int mrkid = 0;
        public Model Mod_el
        {
            get { return model; }
            set { model = value; }
        }
        private GroupBox groupBox1;
        private TextBox txtarama;
        private Button btniptal;
        private Button btnsil;
        private Button btnTamam;
        private Button btnekle;
        private Button btnduzenle;
        private GroupBox gbmalzeme;
        private ListView listmodel;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;


        public Form_Model()
        {
            InitializeComponent();
            db = new DepodataDataContext();
        }

        public Form_Model(ref DepodataDataContext Db, int p)
        {
            InitializeComponent();
            db = Db;
            mrkid = p;
            var soru = from x in db.Models
                       where x.markaid == p
                       orderby x.Adi
                       select x;
            Yenile(soru);
        }

        private void Yenile(IOrderedQueryable<Model> soru)
        {
            listmodel.Items.Clear();
            foreach (var item in soru)
            {
                ListViewItem litem = new ListViewItem();
                litem.Text = item.id.ToString();
                litem.SubItems.Add(item.Adi + " " + item.Modelno);
                listmodel.Items.Add(litem);
            }
        }
        private void Yenile()
        {
            listmodel.Items.Clear();
            IQueryable<Model> soru;
            if (mrkid < 1)
            {
                soru = from x in db.Models
                       orderby x.Adi
                       orderby x.Modelno
                       select x;
            }
            else
            {
                soru = from x in db.Models
                       where x.markaid == mrkid
                       orderby x.Adi
                       orderby x.Modelno
                       select x;

            }

            foreach (var item in soru)
            {
                ListViewItem litem = new ListViewItem();
                litem.Text = item.id.ToString();
                litem.SubItems.Add(item.Adi + " " + item.Modelno);
                listmodel.Items.Add(litem);
            }
        }

        private void lblekle_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            btnekle.Enabled = true;
            btnekle.Tag = 1;
            btnekle.Text = "Ekle";
        }

        private void lblduzenle_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            btnekle.Text = "Değiştir";
            btnekle.Tag = 0;
            btnekle.Enabled = true;
            btnsil.Enabled = true;
        }

        private void listmalzeme_SelectedIndexChanged(object sender, EventArgs e)
        {
            Mod_el = (from x in db.Models where x.id == int.Parse(listmodel.SelectedItems[0].Text) select x).FirstOrDefault();

        }





        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.txtarama = new System.Windows.Forms.TextBox();
            this.btniptal = new System.Windows.Forms.Button();
            this.btnsil = new System.Windows.Forms.Button();
            this.btnTamam = new System.Windows.Forms.Button();
            this.btnekle = new System.Windows.Forms.Button();
            this.btnduzenle = new System.Windows.Forms.Button();
            this.gbmalzeme = new System.Windows.Forms.GroupBox();
            this.listmodel = new System.Windows.Forms.ListView();
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.groupBox1.SuspendLayout();
            this.gbmalzeme.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.txtarama);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(280, 39);
            this.groupBox1.TabIndex = 12;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Arama";
            // 
            // txtarama
            // 
            this.txtarama.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtarama.Location = new System.Drawing.Point(3, 16);
            this.txtarama.Name = "txtarama";
            this.txtarama.Size = new System.Drawing.Size(274, 20);
            this.txtarama.TabIndex = 0;
            this.txtarama.TextChanged += new System.EventHandler(this.txtarama_TextChanged);
            // 
            // btniptal
            // 
            this.btniptal.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btniptal.Location = new System.Drawing.Point(232, 446);
            this.btniptal.Name = "btniptal";
            this.btniptal.Size = new System.Drawing.Size(45, 38);
            this.btniptal.TabIndex = 10;
            this.btniptal.Text = "İptal";
            this.btniptal.UseVisualStyleBackColor = true;
            this.btniptal.Click += new System.EventHandler(this.btniptal_Click);
            // 
            // btnsil
            // 
            this.btnsil.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnsil.Enabled = false;
            this.btnsil.Location = new System.Drawing.Point(125, 446);
            this.btnsil.Name = "btnsil";
            this.btnsil.Size = new System.Drawing.Size(34, 25);
            this.btnsil.TabIndex = 11;
            this.btnsil.Text = "Sil";
            this.btnsil.UseVisualStyleBackColor = true;
            // 
            // btnTamam
            // 
            this.btnTamam.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnTamam.Enabled = false;
            this.btnTamam.Location = new System.Drawing.Point(165, 446);
            this.btnTamam.Name = "btnTamam";
            this.btnTamam.Size = new System.Drawing.Size(61, 38);
            this.btnTamam.TabIndex = 9;
            this.btnTamam.Text = "Tamam";
            this.btnTamam.UseVisualStyleBackColor = true;
            this.btnTamam.Click += new System.EventHandler(this.btnTamam_Click);
            // 
            // btnekle
            // 
            this.btnekle.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnekle.Location = new System.Drawing.Point(0, 446);
            this.btnekle.Name = "btnekle";
            this.btnekle.Size = new System.Drawing.Size(51, 25);
            this.btnekle.TabIndex = 7;
            this.btnekle.Text = "Ekle";
            this.btnekle.UseVisualStyleBackColor = true;
            this.btnekle.Click += new System.EventHandler(this.btnekle_Click);
            // 
            // btnduzenle
            // 
            this.btnduzenle.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnduzenle.Enabled = false;
            this.btnduzenle.Location = new System.Drawing.Point(57, 446);
            this.btnduzenle.Name = "btnduzenle";
            this.btnduzenle.Size = new System.Drawing.Size(62, 25);
            this.btnduzenle.TabIndex = 8;
            this.btnduzenle.Text = "Düzenle";
            this.btnduzenle.UseVisualStyleBackColor = true;
            // 
            // gbmalzeme
            // 
            this.gbmalzeme.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.gbmalzeme.Controls.Add(this.listmodel);
            this.gbmalzeme.Location = new System.Drawing.Point(2, 45);
            this.gbmalzeme.Name = "gbmalzeme";
            this.gbmalzeme.Size = new System.Drawing.Size(278, 395);
            this.gbmalzeme.TabIndex = 6;
            this.gbmalzeme.TabStop = false;
            this.gbmalzeme.Text = "Markalar";
            // 
            // listmodel
            // 
            this.listmodel.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2});
            this.listmodel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listmodel.FullRowSelect = true;
            this.listmodel.Location = new System.Drawing.Point(3, 16);
            this.listmodel.MultiSelect = false;
            this.listmodel.Name = "listmodel";
            this.listmodel.Size = new System.Drawing.Size(272, 376);
            this.listmodel.TabIndex = 0;
            this.listmodel.UseCompatibleStateImageBehavior = false;
            this.listmodel.View = System.Windows.Forms.View.Details;
            this.listmodel.SelectedIndexChanged += new System.EventHandler(this.listmodel_SelectedIndexChanged);
            this.listmodel.DoubleClick += new System.EventHandler(this.listmodel_DoubleClick);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "ID";
            this.columnHeader1.Width = 50;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "Model / Model No";
            this.columnHeader2.Width = 201;
            // 
            // Form_Model
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(280, 486);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.btniptal);
            this.Controls.Add(this.btnsil);
            this.Controls.Add(this.btnTamam);
            this.Controls.Add(this.btnekle);
            this.Controls.Add(this.btnduzenle);
            this.Controls.Add(this.gbmalzeme);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.KeyPreview = true;
            this.Name = "Form_Model";
            this.Text = "Model";
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Model_KeyDown);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.gbmalzeme.ResumeLayout(false);
            this.ResumeLayout(false);

        }




        private void btnTamam_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();

        }


        private void listmodel_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnTamam.Enabled = true;
            btnduzenle.Enabled = true;
            btnsil.Enabled = true;

            try
            {
                model = (from x in db.Models where x.id == int.Parse(listmodel.SelectedItems[0].Text) select x).FirstOrDefault();
            }
            catch (Exception)
            {

            }
        }

        private void listmodel_DoubleClick(object sender, EventArgs e)
        {
            btnTamam_Click(null, null);
        }

        private void btnekle_Click(object sender, EventArgs e)
        {
            Form_ModelGirdisi frm = new Form_ModelGirdisi(ref db, mrkid);
            if (frm.ShowDialog () == DialogResult.OK)
            {
                model = frm.Model;
                Yenile();
            }
        }

        private void txtarama_TextChanged(object sender, EventArgs e)
        {
            IOrderedQueryable<Model> soru;
            listmodel.Items.Clear();
            if (mrkid > 0)
            {
                soru = from x in db.Models
                       where x.markaid == mrkid
                       where x.Adi.ToUpper().Contains(txtarama.Text.ToUpper())
                       orderby x.Adi
                       select x;

            }
            else
            {
                soru = from x in db.Models
                       where x.Adi.ToUpper().Contains(txtarama.Text.ToUpper())
                       orderby x.Adi
                       select x;


            }
            Yenile(soru);

        }

        private void btniptal_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void Form_Model_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Escape)
            {
                btniptal_Click(null, null);
            }
        }


    }
}
